﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZetaPrints.ZetaClient.Presentation
{
    /// <summary>
    /// Displays a date and time with e default entry entered
    /// </summary>
    public partial class FormDownloadDate : Form
    {
        #region public properties
        /// <summary>
        /// Value that was selected on the press of the button
        /// </summary>
        public DateTime ResultDateTime{get;set;}
        #endregion


        #region constuctors
        /// <summary>
        /// Initialize the download form
        /// </summary>
        /// <param name="defaultDateTime"></param>
        public FormDownloadDate(DateTime defaultDateTime)
        {
            InitializeComponent();

            dateTimePickerDownload.Value = defaultDateTime;

            textBoxTime.Text = defaultDateTime.ToString("hh:mm:ss tt");

            AcceptButton = buttonOK;
            CancelButton = buttonCancel;
        }
        #endregion


        #region event handlers
        /// <summary>
        /// When we press the OK button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            bool validTime = true;

            ResultDateTime = DateTime.Parse(dateTimePickerDownload.Value.ToString("MM/dd/yyyy"));

            int hours = 0;
            int minute = 0;
            int second = 0;
            if (textBoxTime.Text.ToUpper().Contains("PM"))
            {
                hours += 12;
            }

            string[] timeFields = textBoxTime.Text.Split(':', ' ');

            for (int timeFieldCount = 0; timeFieldCount < timeFields.Length; timeFieldCount++)
            {
                switch (timeFieldCount)
                {
                    case 0:
                        hours += Convert.ToInt32(timeFields[timeFieldCount]);
                        if ((hours > 24) || (hours < 0))
                        {
                            validTime = false;
                            break;
                        }
                        break;
                    case 1:
                        minute += Convert.ToInt32(timeFields[timeFieldCount]);
                        if ((minute < 0) || (minute > 59))
                        {
                            validTime = false;
                            break;
                        }
                        break;
                    case 2:
                        if ((!timeFields[timeFieldCount].ToUpper().Contains("AM")) &&
                            (!timeFields[timeFieldCount].ToUpper().Contains("PM")))
                        {
                            second += Convert.ToInt32(timeFields[timeFieldCount]);
                            if ((second < 0) || (second > 59))
                            {
                                validTime = false;
                                break;
                            }
                        }
                        break;
                }
            }

            ResultDateTime.AddHours(hours);
            ResultDateTime.AddMinutes(minute);
            ResultDateTime.AddMinutes(second);

            if (validTime)
            {
                DialogResult = DialogResult.OK;
                Visible = false;
            }
            else
            {
                MessageBox.Show(this, "Invalid Time, please try again", "Invalid time", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// When we press the Cancel button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Visible = false;
        }
        #endregion
    }
}
